<?php

namespace Biz\model\wiki;

use Biz\tools\helper\Common;
use Doraemon\tools\ImgUrl;

class WikiBrandAreaModule extends \Doraemon\pockets\datebase\BaseDbModel
{
    const _CONFIG_FILE_ = 'db_store';

    const _CONFIG_SELECT_ = 'db_store';

    const _DATABASE_ = 'store';

    const _TABLE_NAME = 'wiki_brand_area_module';

    const STATUS_NORMAL = 1;
    const STATUS_DELETE = 9;

    const MODULE_TYPE_VIDEO = 1;//视频
    const MODULE_TYPE_TOPIC = 2;//话题
    const MODULE_TYPE_TEST = 3;//评测
    const MODULE_TYPE_H5 = 4;//h5
    const ADM_MODULE_TYPE_MAP = [
        self::MODULE_TYPE_VIDEO => '视频',
        self::MODULE_TYPE_TOPIC => '话题',
        self::MODULE_TYPE_TEST => '评测',
        self::MODULE_TYPE_H5 => 'h5',
    ];
    public static function getTypeName($type, $default = '')
    {
        return self::ADM_MODULE_TYPE_MAP[$type] ?? $default;
    }

    public static function admGetInfo($id)
    {
        $where = [
            'id' => $id,
        ];
        $row = self::get('*', $where);
        if ($row['link']) {
            $row['h5_link'] = self::formatH5($row['link']);
        }
        return $row;
    }
    public static function formatH5($h5)
    {
//        return 'hhz://urlb:' . base64_encode($h5);
        return Common::formatLinkNormalLink($h5);
    }

    public static function admDelModule($id)
    {
        $where = [
            'id' => $id,
        ];
        $data['edittime'] = date("Y-m-d H:i:s");
        $data['module_status'] = self::STATUS_DELETE;
        return self::update($data, $where)->rowCount();
    }

    public static function admSaveInfo($id, $data)
    {
        if (self::admGetInfo($id)) {
            $where = [
                'id' => $id,
            ];
            $data['edittime'] = date("Y-m-d H:i:s");
            return self::update($data, $where)->rowCount();
        } else {
            $data['module_status'] = self::STATUS_NORMAL;
            $data['addtime'] = date("Y-m-d H:i:s");
            self::insert($data);
            return self::id();
        }
    }

    public static function admModuleList($brand_id)
    {
        $where = [
            'brand_id' => $brand_id,
            'module_status' => self::STATUS_NORMAL,
        ];
        $list = self::select('*', $where);
        if ($list) {
            $list = self::admFormat($list);
        }
        return $list;
    }

    public static function admFormat($list)
    {
        $response = [];
        foreach ($list as $v) {
            $v['module_type_desc'] = self::getTypeName($v['module_type'], '-');
            $v['link'] = $v['link'] ? $v['link'] : '-';
            $v['unique_id'] = $v['unique_id'] ? $v['unique_id'] : '-';
            $response[] = $v;
        }
        return $response;
    }

    public static function admCheckSort($brand_id, $type, $sort)
    {

    }

    public static function getAllByWhere($where = [],$files = '*')
    {
        if(!empty($where)){
            $where = array_merge($where, ['module_status'=>self::STATUS_NORMAL]);
        }else{
            $where =  ['module_status'=>self::STATUS_NORMAL];
        }
        $data = self::select($files, $where);
        $data = array_map(function ($_data){
            return self::_filter($_data);
        },$data);
        return $data;
    }

    public static function _filter($data){
        foreach($data as $_k=>&$_v){
            switch ($_k){
                case 'pic_id':
                    $data['pic_url'] = !empty($_v) ? ImgUrl::getUrl(ImgUrl::APPTYPE_OP, $_v, ImgUrl::IMAGE_SIZE_OP_ORI) : '';
                    break;
            }
        }
        return $data;
    }

    public static function getOneByWhere( $where = [], $files = '*')
    {
        $data = self::get($files, $where);
        return $data;
    }

    public static function addData($data)
    {
        try {
            self::insert($data);
            return self::id();
        } catch (Exception $e) {
            return false;
        }
    }

    public static function updateData($data, $where)
    {
        try {
            self::update($data, $where);
            return true;
        } catch (Exception $e) {
            return false;
        }
    }

}